<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/29
 * Time: 18:15
 */

namespace app\pipeline\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\company\model\Company as CompanyModel;
use app\pipeline\model\Equipment as EquipmentModel;
use app\pipeline\model\Business as BusinessModel;

class Equipment extends Admin
{

    public function index(){
        $map=$this->getMap();
        if($this->user_auth['role']!==1&&$this->user_auth['company_id']!==0){
            $map[]=['company_id','=',$this->user_auth['company_id']];
        }
        $data_list = EquipmentModel::where($map)->order('id','desc')->paginate()->each(function($value){
            if($value['company_id']){
                $value['company_id']=CompanyModel::where('id',$value['company_id'])->value('name');
            }
        });
        return ZBuilder::make('table')
            ->setPageTitle('设备管理') // 页面标题
            ->setTableName('equipment') // 设置表名
            ->setSearch(['name' => '设备名称', 'id' => 'ID','number'=>'设备编号']) // 设置搜索参数
            ->addColumns([ // 批量添加列
                ['id', 'ID'],
                ['name', '设备名称'],
                ['number', '设备编号'],
                ['company_id', '所属公司'],
                ['use_time', '使用次数'],
                ['create_time', '创建时间'],
                ['update_time', '修改时间'],
                ['right_button', '操作', 'btn']
            ])
            ->addRightButtons(
                [
                    'edit'=> ['title'=>'编辑','href' => url('equipment_save', ['id' => '__id__'])],
                    'delete'=>['title'=>'删除','href'=>url('equipment_del',['id'=>'__id__'])]
                ]
            )
            ->addTopButton('add',['href'=>url('equipment_save')])
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染模板
    }

    public function equipment_save(){
        if($this->request->isPost()){
            if($this->user_auth['role']!==1){
                $this->error('无权操作');
            }
            $data=$this->request->post();
            if($this->user_auth['role']!==1&&$this->user_auth['company_id']!==0){
                $data['company_id']=$this->user_auth['company_id'];
            }
            // 验证
            $result = $this->validate($data, 'Equipment');
            if(true !== $result) $this->error($result);
            if(!empty($data['id'])){
                $result=EquipmentModel::update($data);
                action_log('equipment_edit','equipment',$data['id'],UID,json_encode($data,JSON_UNESCAPED_UNICODE));
            }else{
                $result=EquipmentModel::create($data);
                action_log('equipment_add','equipment',$result['id'],UID,$data['name']);
            }
            if(!$result){
                $this->error('保存失败');
            }
            $this->success('保存成功','index');
        }
        $id=$this->request->param('id');
        $info=EquipmentModel::get($id);
        $company_list=CompanyModel::where('company_type','neq','third_slave')->column('name','id');
        // 显示编辑页面
        return ZBuilder::make('form')
            ->setPageTitle('编辑设备信息')
            ->setPageTips('请谨慎操作','warning')
            ->addFormItems([
                ['hidden', 'id'],
                ['select', 'company_id', '公司选择', '<span class="text-danger">必填</span>', $company_list],
                ['text', 'name', '设备名称', '<span class="text-danger">必填</span>'],
                ['text', 'number', '设备编号', '<span class="text-danger">必填</span>'],
                ['text','type','设备型号'],
                ['text','accuracy','设备精确度'],
                ['text','other','设备参数等(待补充)']
            ])
            ->setFormData($info)
            ->fetch();
    }

    public function equipment_del(){
        if($this->user_auth['role']!==1&&$this->user_auth['company_id']!==0){
            $this->error('无权操作');
        }
        $id=$this->request->param('id');
        $has_equip=EquipmentModel::where("id",$id)->find();
        if(!$has_equip){
            $this->error('未找到指定设备信息');
        }
        $has_business=BusinessModel::where('equipment_number',$id)->count(1);
        if(!$has_business){
            $this->error('当前设备已有业务使用,无法删除');
        }
        $del_result=EquipmentModel::where("id",$id)->delete();
        if(!$del_result){
            $this->error('删除设备失败');
        }

        $this->success('删除设备成功');
        action_log('equipment_del','equipment',$id,UID,$has_equip);
    }



}